package com.basic.business.wholeIndicators.controller;


import com.basic.business.wholeIndicators.domain.TWholeStationIndicators;
import com.basic.business.wholeIndicators.service.ITWholeStationIndicatorsService;
import com.basic.common.annotation.FileExport;
import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.enums.BusinessType;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import com.google.common.util.concurrent.RateLimiter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 全站推指标数据Controller
 *
 * @author zhaoxy_jn@163.com
 */
@RestController
@RequestMapping("/business/wholeStationIndicators")
public class TWholeStationIndicatorsController extends BaseController
{
    @Resource
    private ITWholeStationIndicatorsService tWholeStationIndicatorsService;

    // 每分钟 30 次调用
    private final RateLimiter rateLimiter = RateLimiter.create(30.0 / 60);

    /**
     * 查询全站推指标数据列表
     */
    @PreAuthorize("@ss.hasPermi('business:wholeStationIndicators:list')")
    @GetMapping("/list")
    public TableDataInfo list(TWholeStationIndicators tWholeStationIndicators)
    {
        startPage();
        List<TWholeStationIndicators> list = tWholeStationIndicatorsService.getList(tWholeStationIndicators);
        return getDataTable(list);
    }

    /**
     * 导出全站推指标数据列表
     */
    @PreAuthorize("@ss.hasPermi('business:wholeStationIndicators:export')")
    @Log(title = "全站推指标数据", businessType = BusinessType.EXPORT)
    @FileExport(fileName = "全站推指标数据.xlsx")
    @PostMapping("/export")
    public String export(TWholeStationIndicators tWholeStationIndicators) throws IOException
    {
        List<TWholeStationIndicators> list = tWholeStationIndicatorsService.getList(tWholeStationIndicators);
        return EasyExcelUtils.excelExport(TWholeStationIndicators.class,"全站推指标数据数据", list);
    }

    /**
     * 获取全站推指标数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:wholeStationIndicators:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tWholeStationIndicatorsService.get(id));
    }

    /**
     * 新增全站推指标数据
     */
    @PreAuthorize("@ss.hasPermi('business:wholeStationIndicators:add')")
    @Log(title = "全站推指标数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWholeStationIndicators tWholeStationIndicators)
    {
        return toAjax(tWholeStationIndicatorsService.insert(tWholeStationIndicators));
    }

    /**
     * 修改全站推指标数据
     */
    @PreAuthorize("@ss.hasPermi('business:wholeStationIndicators:edit')")
    @Log(title = "全站推指标数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWholeStationIndicators tWholeStationIndicators)
    {
        return toAjax(tWholeStationIndicatorsService.update(tWholeStationIndicators));
    }

    /**
     * 删除全站推指标数据
     */
    @PreAuthorize("@ss.hasPermi('business:wholeStationIndicators:remove')")
    @Log(title = "全站推指标数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tWholeStationIndicatorsService.deleteByIds(ids));
    }

    /**
     * 上传全站推指标数据
     * @param file
     * @return
     */
    @PostMapping("/uploadData")
    public AjaxResult uploadData(MultipartFile file, String wholeStoreId, Date wholeSelectDate, String wholeFileHash) {

        if (!rateLimiter.tryAcquire()) {
            return AjaxResult.error("请求过多，请稍后再试!");
        }else {
            tWholeStationIndicatorsService.uploadIndicatorsData(file, wholeSelectDate, wholeStoreId, wholeFileHash);
            return AjaxResult.success();
        }
    }
}
